<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/30
 * Time: 9:07
 */

namespace app\home\controller;


class Second extends Base
{
    public function _initialize(){
        parent::_initialize();
        $id = input('param.id/d',0);

        $this->assign('id',$id);
    }
    //楼盘列表页
    public function index(){
        $this->search();
        $this->getAttr();
        $this->getLists();
        $this->assign('price',$this->getPrice());
        $this->assign('mianji',$this->getMianji());
        $this->assign('room',$this->getRoom());
        $this->assign('house_type',cache('linkmenu_7')['cate']);
        $this->assign('zhuangxiu',cache('linkmenu_9')['cate']);
        $this->assign('chaoxiang',cache('linkmenu_12')['cate']);
        return $this->fetch();
    }
    //楼盘详细页
    public function detail(){
        $id   = input('param.id/d',0);
        $info = model('second_house')->where(['status'=>1,'id'=>$id])->find();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $estate  = model('estate_data')->where(['id'=>$info['estate_id']])->value('info');
        $explain = cache('explain');
        $this->setSeo($info);
        model('second_house')->where(['id'=>$id])->setInc('hits');
        if($info['user_id']){
            $this->getUserinfo($info['user_id']);
        }
        $this->assign('zhuangxiu',$this->getAttrName(9,$info['zhuangxiu']));
        $this->assign('chaoxiang',$this->getAttrName(12,$info['chaoxiang']));
        $this->assign('peitao',$this->getAttrName(13,$info['peitao']));
        $this->assign('house_type',cache('linkmenu_7')['cate']);
        $this->assign('info',$info);
        $this->assign('explain',$explain['second']);
        $this->assign('estate',$estate);
        return $this->fetch();
    }
    private function getUserinfo($id){
        $info = model('user')->where(['id'=>$id])->field('id,nickname,headimgurl,cardimg,buy_phone,company,level')->find();
        $this->assign('user',$info);
    }
    private function getLists(){
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,img,address,price,total_price,room,zhuangxiu,chaoxiang,ting,mianji,house_type,floor,total_floor,contact,contact_phone';
        $lists = model('second_house')->where($where)->field($field)->order($sort)->paginate(10);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
    }
    private function search(){
        $param['area'] = input('param.area',0);
        $param['price']      = input('param.price',0);
        $param['mianji']     = input('param.mianji',0);
        $param['room']       = input('param.room',0);//户型
        $param['chao']       = input('param.chao',0);//楼盘朝向
        $param['sort']       = input('param.sort/d',0);//排序
        $data['status']      = 1;
        $data['city']        = $this->cityInfo['id'];
        $keyword = input('get.keyword');
        if($keyword){
            $param['keyword'] = $keyword;
            $data['title'] = ['like','%'.$keyword.'%'];
        }

        if(!empty($param['area'])){
            $data['area'] = $param['area'];
        }
        if(!empty($param['price'])){
            $data['total_price'] = $this->getPrice($param['price']);
        }
        if(!empty($param['mianji'])){
            $data['mianji'] = $this->getMianji($param['mianji']);
        }
        if(!empty($param['room'])){
            $data['room'] = $param['room'];
        }
        if(!empty($param['chao'])){
            $data['chaoxiang'] = $param['chao'];
        }
        $this->assign('param',$param);
        $this->assign('search',$param);
        return $data;
    }
    private function getAttr($return=''){
        $chaoxiang     = cache('linkmenu_12');//朝向
        if($return){
          return $chaoxiang['cate'];
        }else{
            $this->assign('chaoxiang',$chaoxiang['cate']);
        }
    }
    private function getAttrName($cate_id,$id){
        $cate = cache('linkmenu_'.$cate_id);
        $cate = $cate['cate'];
        if(is_numeric($id)){
            return $cate[$id]['name'];
        }
        $id_arr = explode(',',$id);
        $temp = [];
        if($id){
            if($id_arr){
                foreach($id_arr as $v){
                    $temp[] = '<li>'.$cate[$v]['name'].'</li>';
                }
            }
        }
        return implode('',$temp);
    }
    public function getPrice($key=''){
        return getSecondPrice($key);
    }
    //面积
    public function getMianji($key=''){
        $data = [
            1 => ['name'=>'50平米以下','value'=>[0,50]],
            2 => ['name'=>'50-70平米','value'=>[50,70]],
            3 => ['name'=>'70-90平米','value'=>[70,90]],
            4 => ['name'=>'90-110平米','value'=>[90,110]],
            5 => ['name'=>'110-130平米','value'=>[110,130]],
            6 => ['name'=>'130-150平米','value'=>[130,150]],
            7 => ['name'=>'150平米以上','value'=>'150']
        ];
        if($key){
            if($key == 7){
                $str = ['egt',$data[$key]['value']];
            }else{
                $str = ['between',$data[$key]['value']];
            }
        }else{
            $str = $data;
        }
        return $str;
    }
    //户型
    private function getRoom($key=0){
        return getRoom($key);
    }
    //排序
    private function order(){
        $sort = input('param.sort/d',0);
        switch($sort){
            case 1:
                $order = 'total_price asc';
                break;
            case 2:
                $order = 'total_price desc';
                break;
            case 3:
                $order = 'price asc';
                break;
            case 4:
                $order = 'price desc';
                break;
            case 5:
                $order = 'mianji asc';
                break;
            case 6:
                $order = 'mianji desc';
                break;
            default:
                $order = 'ordid asc,id desc';
                break;
        }
        $this->assign('sort',$sort);
        return $order;
    }

}